package com.hydra.auth.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.hydra.auth.common.AuthConstant;
import com.hydra.base.comment.Comment;
import com.hydra.base.entity.CodeUserTimeEntity;
import com.hydra.base.operate.log.annotation.OperateLogProperty;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.List;

/**
 * 系统用户
 * 用来登录的，以后其他系统的用户可以继承这个对象
 * @Author Hydra
 * @Date 2022/3/11 17:09
 */
@ToString(callSuper = true)
@Comment("系统用户表")
@TableName(value = "tb_sys_user")
@Accessors(chain = true)
public class SysUser extends CodeUserTimeEntity implements UserDetails {

    /**
     * 用户名
     */
    @Getter @Setter
    @OperateLogProperty("用户名")
    String username;

    /**
     * 密码
     */
    @Getter @Setter
    String password;

    /**
     * 邮箱
     */
    @Getter @Setter
    @OperateLogProperty("邮箱")
    String email;

    /**
     * 手机号
     */
    @Getter @Setter
    @OperateLogProperty("手机号")
    String phone;

    /**
     * 用户昵称
     */
    @Getter @Setter
    @OperateLogProperty("用户昵称")
    String name;

    /**
     * 头像文件id
     */
    @Getter @Setter
    @Comment("头像文件id")
    Long imageId;

    /**
     * 账户状态
     * 1：启用，0：禁用
     */
    @Getter @Setter
    @OperateLogProperty("状态")
    String status;

    /**
     * 系统编码
     */
    @Getter @Setter
    @OperateLogProperty("系统编码")
    String systemCode;

    /**
     * 注册平台
     * Web、H5、MiniProgram、Android、IOS
     */
    @Getter @Setter
    @OperateLogProperty("注册平台")
    String platform;

    /**
     * 备注
     */
    @Getter @Setter
    @OperateLogProperty("备注")
    String remark;

    /**
     * 用户具备的权限
     */
    @ToString.Exclude
    @Getter @Setter
    @TableField(exist = false)
    List<SysRole> roles;


    /**security相关方法**/
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.roles;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return AuthConstant.USER_STATUS_ON.equals(this.status);
    }
}
